package com.qzdsoft.erpcloud.controller.stock;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.stock.StockOutbound;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.sale.SaleOrderService;
import com.qzdsoft.erpcloud.service.stock.StockOutBoundService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundEditInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundQueryInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;
import com.qzdsoft.vo.stock.StockOutType;

@Controller
@RequestMapping("/stock/outbound")
public class StockOutBoundController extends BaseController
{   
    private static final Logger logger = LoggerFactory.getLogger(StockOutBoundController.class);
    private static final String BASEPATH = "stock/outbound";
    
    @Autowired
    StockOutBoundService stockOutBoundService;
    @Autowired
    CustomerSupplierService customerSupplierService;
    @Autowired
    CustomerSupplierStatementService customerSupplierStatementService;
    @Autowired
    SaleOrderService saleOrderService;
    
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("company", this.getUser().getCompanys());
        mv.addObject("companyId", this.getUser().getCompanyId());
        mv.addObject("status",status);
        mv.setViewName(BASEPATH+"/index");
        return mv;
    }
    /**
     * 按条件获取出库单
     * @param info
     * @return
     */
    @ResponseBody
    @SysLog(description="查询销售出库单")
    @RequestMapping("/list")
    public LayTableResponse<StockOutboundInfo> list(StockOutboundQueryInfo info){
        logger.debug("查询销售出库单，参数{}"+info);
        if(info.isFirst()) {
            return LayTableResponse.nullDataPage();
        }else {
            info.setOutboundType(StockOutType.SALEOUT_CODE);   //设置为销售出库
            return stockOutBoundService.findPurchaseOrdersBy(info);
        }
    }
    
    /**
     * 转换视图
     * @param companyid
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/{companyid}/{id}")
    public ModelAndView detail(@PathVariable("companyid")String companyid,@PathVariable("id")Integer id) {
        ModelAndView mv = new ModelAndView();
        
       if("view".equals(companyid)) {
           mv.setViewName(BASEPATH + "/view");
       }else{
        logger.debug("查询销售出库单详情：companyid={},id={}",companyid,id);
        StockOutboundInfo order = stockOutBoundService.findOrdersBy(id);
        mv.addObject("order", order);
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status", status);
        mv.setViewName(BASEPATH + "/edit");
       }
        
        return mv;
    }
    /**
     * 新增页面
     * @param companyId
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/add")
    public ModelAndView add(Integer companyId) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("typeInfo",saleOrderService.queryBillNo(companyId));//已审核销售订单
        mv.addObject("company", this.getUser().getCompanys());
        mv.addObject("companyId", this.getUser().getCompanyId());
        mv.setViewName(BASEPATH+"/add");
        
        return mv;
    }
    
    /**
     * 查询来源出库单号信息
     * @param id 来源单编号
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @SysLog(description="查询来源单据号")
    @RequestMapping("/saleOrder/{id}")
    public Response getpurchaseOrderById(@PathVariable("id")Integer id) {
        //查询来源单据号
        logger.debug("查询来源单据号:sourceid={}",id);
        return new Response(ResultEnum.SUCCESS,saleOrderService.findSaleOrderBy(id));
    }
    
    /**
     * 编辑修改
     * @param info
     * @return
     */
    @ResponseBody
    @SysLog(description="修改销售出库单")
    @RequestMapping("/edit")
    public Response<StockOutboundInfo> edit(StockOutboundEditInfo info){
        logger.debug("修改销售出库单,内容 "+info);
        return stockOutBoundService.edit(info);
    }
    
    /**
     * 新增保存
     * @param info
     * @return
     */
    @ResponseBody
    @SysLog(description="新增销售出库单")
    @RequestMapping("/save")
    public Response<String> save(StockOutbound info){
        logger.debug("新增销售出库单,内容 "+info);
        info.setOutboundType(StockOutType.SALEOUT_CODE);//保存为销售出库
        return stockOutBoundService.save(info);
    }
    
    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="删除销售出库单")
    @RequestMapping("/del")
    public Response<String> detele(Integer id) {
        logger.debug("删除销售出库单,id : "+id);
        return stockOutBoundService.delete(id);
    }
    
    /**
     * 审核、弃审、关闭
     * @param id
     * @param status
     * @return
     */
    @ResponseBody
    @SysLog(description="审核销售出库单")
    @RequestMapping("/review")
    public Response<String> review(Integer id,Integer status) {
        logger.debug("审核销售出库单,id"+id);
        return stockOutBoundService.review(id,status);
    }
    /**
     * 销售订单转销售出库单
     * @param id 销售订单id
     * @return
     */
    
    @RequestMapping("/saleSave")
    public ModelAndView saleSave(Integer id) {
        ModelAndView mv = new ModelAndView();
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status",status);
        mv.setViewName(BASEPATH+"/index");
        return mv;
    }
    
    /**
     * 客户税率信息查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/queryTax/{supplierid}/{companyid}")
    public TypeInfo queryTax(@PathVariable("supplierid")Integer supplierid,@PathVariable("companyid")Integer companyid) {
        return customerSupplierService.selectTaxInfo(companyid, supplierid);
    }
    
    /**
     * 客户结算方式查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/querySettleMethod/{supplierid}")
    public List<CustomerSupplierStatementInfo> querySettleMethod(@PathVariable("supplierid")Integer supplierid) {
        //客户结算方式
       Integer companyid = this.getUser().getCompanyId();
       List<CustomerSupplierStatementInfo> customerSupplierStatements = customerSupplierStatementService.selectBy(companyid, supplierid);
       return customerSupplierStatements;
    }
    
}
